<?php

namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class UserController extends AdminbaseController {

	function _initialize() {
		parent::_initialize ();
		$this->db = D ( "Users" );
		$this->group = D ( "Group" );

	}
	function index() {


		if(I("get.group")){
			$map['group']=I("get.group") ;

			$search=true;
		}





		if(I("get.keyword")){
		
			
			$map['nickname']  = array('like','%'.I("get.keyword").'%');
			$map['tel']  = array('like','%'.I("get.keyword").'%');
			$map['_logic'] = 'or';
			
			
		
		}
		$this->assign("keyword",I("get.keyword"));
		$count=$this->db->where($map)->count();
		$page = $this->page($count);
		$list=$this->db->limit($page->firstRow . ',' . $page->listRows)->order ( "id desc ")->where($map)->select();
		
		$this->assign("Page",$page->show(""));
		
		$this->assign("_list",$list);
		
		$this->display();
		
		
		
		
		
	
	}

	function shenhe() {


		if(I("get.keyword")){


			$map['name']  = array('like', '%'.I("get.keyword").'%');




		}
		$this->assign("keyword",I("get.keyword"));


		$map["shenhe"]=0;

		$list=$this->db->order ( "id desc ")->where($map)->select();



		$this->assign("_list",$list);

		$this->display();






	}
	function add() {
		if (IS_POST) {
			
			$_POST[pwd_hash]=rand_string();
			$_POST[pass]=lap_password($_POST[pass],$_POST[pwd_hash]);
			if ($this->db->create ()) {
				if ($this->db->add ()) {
					
			
					
					
					$this->success ( "添加成功！", U ( "index" ) );
				} else {
					$this->error ( "添加失败！" );
				}
			} else {
				$this->error ( $this->db->getError () );
			}
		} else {



			$groups=$this->group->select();



			$this->assign("groups",$groups);


			$this->display ();
		}
	}

	function edit() {
		if (IS_POST) {
			
			
			if (empty ( $_POST ['pass'] )) {
				unset ( $_POST ['pass'] );
			}else{
				
				$pwd_hash=$this->db->where(array("id"=>$_POST[id]))->getField("pwd_hash");
				$_POST[pass]=lap_password($_POST[pass],$pwd_hash);
				
			}
			
			
			if ($this->db->create ()) {
				
				if ($this->db->save () !== false) {
					
				
					
					$this->success ( "保存成功！" , U ( "index" ) );
				} else {
					$this->error ( "保存失败！" );
				}
			} else {
				$this->error ( $this->db->getError () );
			}
		} else {	
			
			$uid = intval ( I ( "get.id" ) );

			$groups=$this->group->select();
			$this->assign("groups",$groups);
			$user = $this->db->where ( array (
					"id" => $uid 
			) )->find ();
			$this->assign ( $user );
			
			$this->display ();
		}
	}
	
	/**
	 * 删除
	 */
	function delete() {
		$id = intval ( I ( "get.id" ) );
	
		
		if ($this->db->where ( "id=$id" )->delete () !== false) {
			$this->success ( "删除成功！" );
		} else {
			$this->error ( "删除失败！" );
		}
	}

	
	/**
	 * 状态转换
	 */
	public function changestatusemail() {
		$id = $_REQUEST[id];
		$r = $this->db->where(array("id" => $id))->find();
	
		if ($r) {
			$data[email_status] = $r['email_status'] ? 0 : 1;
			$data[id]=$id;
			$result=  $this->db->save($data);
			if ($result !== false) {
	
				$this->success("转换成功！");
	
			} else {
				$this->error("转换失败！");
			}
		} else {
			$this->error("不存在！");
		}
	}


	function cz_log(){


		$where["modal"]="chongzhi";
		$where["status"]=2;

		$search=false;
		if(I("get.keyword")){


			$map['nickname']  = I("get.keyword");


			$search=true;

		}



		if($search){

			$uid=D("Users")->where($map)->getField("id");


			$where["uid"]=$uid;

		}


		$count=D("Order")->where($where)->count();
		$page = $this->page($count);
		$list=D("Order")->where($where)->limit($page->firstRow . ',' . $page->listRows)->order ( "id desc ")->select();

		$this->assign("Page",$page->show(""));

		$this->assign("_list",$list);

		$this->display ();
	}


	function cz_log_admin(){

		$search=false;
		if(I("get.keyword")){


			$map['name']  = I("get.keyword");


			$search=true;

		}


		if(I("get.card")){
			$map['card']=I("get.card") ;

			$search=true;
		}

		if($search){

			$uid=D("Users")->where($map)->getField("id");


			$where["uid"]=$uid;

		}





		$count=D("AdminczLog")->count();
		$page = $this->page($count);
		$list=D("AdminczLog")->limit($page->firstRow . ',' . $page->listRows)->order ( "id desc ")->where($where)->select();

		foreach ($list as $k=>$v) {

			$list[$k]["card"]=D("Users")->where(array("id"=>$v["uid"]))->getField("card");

		}
		$this->assign("Page",$page->show(""));

		$this->assign("_list",$list);

		$this->display ();
	}

	function chongzhi() {
		if (IS_POST) {



			$money=intval(I("post.money"));
			$uid=I("post.id");





			if(!$money){

				$this->error("充值金额不能为空");
			}

			if($money<0){

				$this->error("充值金额有误");
			}



			if (D("Users")->chongzhi($uid,$money)) {


				$data=array("ip"=>get_client_ip(),"inputtime"=>time(),"money"=>$money,"uid"=>$uid);

				D("AdminczLog")->add($data);


				$this->success ( "充值成功！" , U ( "index" ) );
			} else {
				$this->error ( "充值失败");
			}
		} else {

			$uid = intval ( I ( "get.id" ) );

			$user = $this->db->where ( array (
				"id" => $uid
			) )->find ();
			$this->assign ( $user );

			$this->display ();
		}
	}
	
}